define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'
], function (angular, JSONPrettyFormat) {
    /**
     *  Module
     *
     * Description
     */
    var BusinessModelAttributeModule = angular.module('BusinessModelAttributeModule', ['DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule'
    ]);

    BusinessModelAttributeModule.controller('BusinessModelAttributeController', function ($scope, Resource, $dataSourceManager, GillionMsg, $location) {
        var businessModelAttributeResource = Resource("/grule-manager/businessModelAttribute/:id", {
            id: "@id"
        });
        $scope.businessModelId = $location.search().businessModelId;
        $scope.search = {
            businessModelId:  $scope.businessModelId
        };

        Resource("/grule-manager/businessModel/:id", {id: '@id'}).get({
            id: $scope.businessModelId
        }, function (businessModel) {
            $scope.businessModel = businessModel;
        });

        $scope.searchBusinessModelAttribute = function () {
            $scope.search = $scope.search || {};
            var params = angular.extend($scope.search);
            $dataSourceManager.dataSources.businessModelAttributeSource.doRequestData(1, params);
        }
        $scope.showPageView = function () {
            $scope.showPage = true;
            $scope.showAdd = false;
            $scope.showEdit = false;
        };
        $scope.showEditView = function () {
            $scope.showPage = false;
            $scope.showAdd = false;
            $scope.showEdit = true;
        };

        $scope.businessModelAttributeGridRendered = function (grid) {
            grid.actions.edit = function (row) {
                businessModelAttributeResource.get({
                    id: row.id
                }, function (result) {
                    delete result.$promise;
                    delete result.$resolved;
                    $scope.currId = result.id;
                    delete result.id;
                    $scope.editBusinessModelAttribute = JSONPrettyFormat(result);
                    $scope.showEditView();
                });
            };
        };

        $scope.showAddView = function () {
            $scope.showPage = false;
            $scope.showAdd = true;
            $scope.showEdit = false;
            var templateBusinessModelAttribute = {
                "name": "【必填】",
                "dataType": "Integer|整型 Float|浮点型 String|字符串 Boolean|布尔 Date|时间 Object|对象类型",
                "valueType": "Constant|常量 JDBC|JDBC Service|java接口 Rule|Rule InParameter|传参 Variable|变量 RemoteService|esb",
                "valueExpress": "",
                "businessModelId": $scope.businessModelId,
                "remark": ""
            };
            $scope.addBusinessModelAttribute = JSONPrettyFormat(templateBusinessModelAttribute);
        };

        $scope.createBusinessModelAttribute = function () {
            var businessModelAttribute = JSON.parse($scope.addBusinessModelAttribute);
            businessModelAttributeResource.create(businessModelAttribute, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "保存成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.businessModelAttributeSource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "保存失败，请核对保存信息！");
                }
            });
        };

        $scope.updateBusinessModelAttribute = function () {
            var businessModelAttribute = JSON.parse($scope.editBusinessModelAttribute);
            businessModelAttribute.id = $scope.currId;
            businessModelAttributeResource.update(businessModelAttribute, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "保存成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.businessModelAttributeSource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "更新失败，请核对更新信息！");
                }
            });
        };

        $scope.onRemoveRecord = function (event, row) {
            GillionMsg.confirm('提示信息', '是否确定删除？', function(r){
                if (r) {
                    businessModelAttributeResource.remove({
                        id: row.id
                    }, function (response) {
                        if (response.success) {
                            GillionMsg.alert("提示消息", "删除成功！");
                            $scope.searchBusinessModelAttribute();
                        }
                    });
                }
            });
            return false;
        };

        $scope.showPageView();
    });

    BusinessModelAttributeModule.filter('dataTypeFilter', function() {
        return function(input) {
            var dataTypeDict = {
                'Integer' : '整型',
                'Float' : '浮点型',
                'String' : '字符串',
                'Boolean' : '布尔',
                'Date' : '时间',
                'Object' : '对象类型',
                'List':'数组'
            };
            return dataTypeDict[input] || '';
        };
    });

    BusinessModelAttributeModule.filter('valueTypeFilter', function() {
        return function(input) {
            var valueTypeDict = {
                'Constant' : '常量',
                'JDBC' : 'JDBC',
                'Service' : 'java接口',
                'Rule' :  'Rule',
                'InParameter' : '传参',
                'Variable' : '变量',
                'RemoteService' : 'esb'
            };
            return valueTypeDict[input] || '';
        };
    });
    BusinessModelAttributeModule.filter('valueExpressFilter', function() {
        return function(input) {
            return input.replace("params[0].","").replace("Integer|","").replace("Float|","");
        };
    });
});